package com.anorak.betsy.retro;


import com.github.unidbg.AndroidEmulator;
import com.github.unidbg.linux.android.AndroidEmulatorBuilder;
import com.github.unidbg.linux.android.AndroidResolver;
import com.github.unidbg.linux.android.dvm.*;
import com.github.unidbg.linux.android.dvm.jni.ProxyClassFactory;
import com.github.unidbg.memory.Memory;
import java.io.*;

public class MyAbstractJni {

    private final AndroidEmulator emulator;

    private final DvmClass cSignUtil;
    private final VM vm;

    public MyAbstractJni() {
        emulator = AndroidEmulatorBuilder.for64Bit()
                .setProcessName("com.anjuke.android.app")
                .build();
        Memory memory = emulator.getMemory();
        memory.setLibraryResolver(new AndroidResolver(23));
        vm = emulator.createDalvikVM();
        vm.setDvmClassFactory(new ProxyClassFactory());
        vm.setVerbose(false);
        DalvikModule dm = vm.loadLibrary(new File("musicflit/src/main/resources/lib/libmusic_moment.so"), false);
        cSignUtil = vm.resolveClass("com/music/moment/common/encrypt/CommonEncrypt");
        dm.callJNI_OnLoad(emulator);
    }

    public void destroy() throws IOException {
        emulator.close();
    }

    public String getSign0(String p1) {
        String methodSign = "getSignByKey(Ljava/lang/String;)Ljava/lang/String;";
        StringObject obj = cSignUtil.callStaticJniMethodObject(emulator, methodSign, p1);
        return obj.getValue();
    }

    public synchronized String sign(String p1) {
        return getSign0(p1);
    }


    // qq_320_001Wenx12Arhbl|0.mp3 加密后是 6ecf95c95fea900d5d15ae6eea8fd914
    // qq_320_0009iijj2yhfmx|0.mp3 加密后是 7ad36ba9a71ff73662e3c99afe6567e8
    // qq_4000_0009iijj2yhfmx|0.flac 加 是 7deba24abc62caab71d261234b0249ab
    public static void main(String[] args) throws Exception {
        String p1 = "qq_320_001D51640M6ZeR|0.mp3";
        MyAbstractJni signUtil = new MyAbstractJni();
        String sign = signUtil.sign(p1);
        System.out.println("key="+p1+"&sign=" + sign);
        signUtil.destroy();
    }

}